// pages/vip/index.js
const WXAPI = require('../../wxapi/main')
const CONFIG = require('../../config.js')
const dateUtil = require('../../utils/date.js')
import Toast from 'tdesign-miniprogram/toast/index';

const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    userInfo: {},
    vipList: [],
    interval: null,
  },
  queryVIPList() {



    WXAPI.queryVIPList().then(res => {
      var vipList = res;

      this.setData({
        vipList
      });
      wx.hideNavigationBarLoading();
    }).catch((e) => {
      wx.hideNavigationBarLoading();
    });
  },
  goBuy(e) {

    var { userInfo } = this.data;
    var payment_method = 1
    var data = e.currentTarget.dataset.data;
    if (userInfo.recharge_balance && userInfo.recharge_balance >= data.price)
      payment_method = 4

    WXAPI.buyVip(data.id, {
      user_id: this.data.userInfo.id,
      "renew": true,
      payment_method
    }).then(res => {

      if (res.code == 200) {
        var payment = res.data;
        if (payment==undefined) {
          wx.showToast({
            "title": "续费成功"
          });
         setTimeout(()=>{
          clearInterval(this.data.interval)
          wx.switchTab({
            url: "/pages/my/index"
          })
         },500) 
          return;

        }
        wx.requestPayment({
          appid: payment.appid,
          timeStamp: payment.timeStamp,
          nonceStr: payment.nonceStr,
          package: payment.package,
          signType: payment.signType,
          paySign: payment.paySign,
          success(res) {
            wx.showToast({
              "title": "续费成功"
            });
            this.refreshUser()
            wx.switchTab({
              url: "/pages/my/index"
            })
          },
          fail: (res) => {
            wx.showToast({
              "title": "续费取消"
            });
            clearInterval(this.data.interval)

          }
        });

        var interval = setInterval(() => {
          WXAPI.queryOrderByNo(payment.order_no).then(res => {
            app.refreshData((userInfo) => {
              this.setData({
                userInfo
              })
            });
            if (res.status == 2) {
              clearInterval(this.data.interval)
              wx.switchTab({
                url: "/pages/my/index"
              })
            }
          })
        }, 100)
        this.setData({
          interval
        })

      } else {
        Toast({
          context: this,
          selector: '#t-toast',
          message: "会员订购失败:" + res.message,
        });
      }

      wx.hideNavigationBarLoading();
    }).catch((e) => {
      wx.hideNavigationBarLoading();
    });
  },
  refreshUser(callback) {



    WXAPI.queryUser().then(res => {
      var userInfo = res;
      if (!userInfo && userInfo.id == null) {
        callback();
        return;
      } else {
        // userInfo.vip_expire_time = dateUtil.toDateShort(userInfo.vip_expire_time)
      }
      this.setData({
        userInfo: userInfo,
      });
      wx.setStorageSync("userInfo", userInfo);

      wx.hideNavigationBarLoading();
    }).catch((e) => {
      wx.hideNavigationBarLoading();
    });

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

    var userInfo = wx.getStorageSync("userInfo");
    // userInfo.vip_expire_time = dateUtil.toDateShort(userInfo.vip_expire_time)
    this.setData({
      userInfo
    })
    this.queryVIPList();

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})